package com.xhh.enjoyapp.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.xhh.enjoyapp.Bean.Post;
import com.xhh.enjoyapp.Bean.User;
import com.xhh.enjoyapp.R;
import com.xhh.enjoyapp.adapter.MyPushInvitationAdapter;
import com.yanzhenjie.recyclerview.swipe.SwipeItemClickListener;
import com.yanzhenjie.recyclerview.swipe.SwipeMenu;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuBridge;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuCreator;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuItem;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuItemClickListener;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView;
import com.yanzhenjie.recyclerview.swipe.widget.DefaultItemDecoration;

import java.io.PipedOutputStream;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.UpdateListener;

/**
 * @description 我发布的帖子界面
 * @author: weiXhh
 * @create: 2020-05-15 17:11
 **/
public class MyPushInvitation extends AppCompatActivity {

    private SwipeMenuRecyclerView recyclerView;
    private SwipeRefreshLayout refreshLayout;
    private TextView isEmpty;
    private ImageView back;

    private List<Post> data;
    private MyPushInvitationAdapter myPushInvitationAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_push_invitation);

        initView();

        refresh();

        refreshLayout.setColorSchemeResources(android.R.color.holo_green_light, android.R.color.holo_red_light,android.R.color.holo_blue_light);
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh();
            }
        });

        back.setOnClickListener(click->{
            finish();
        });
    }

    private void refresh() {

        // 获取我发布的帖子
        User user = BmobUser.getCurrentUser(User.class);
        BmobQuery<Post> bmobQuery = new BmobQuery<>();
        bmobQuery.addWhereEqualTo("author", user);
        bmobQuery.order("-createdAt");
        bmobQuery.findObjects(new FindListener<Post>() {
            @Override
            public void done(List<Post> list, BmobException e) {
                refreshLayout.setRefreshing(false);
                if(e == null){
                    data = list;
                    if(data.size() > 0){

                        recyclerView.addItemDecoration(new DefaultItemDecoration(Color.GRAY));
                        recyclerView.setSwipeMenuCreator(swipeMenuCreator);
                        recyclerView.setSwipeMenuItemClickListener(swipeItemClickListener);

                        myPushInvitationAdapter = new MyPushInvitationAdapter(MyPushInvitation.this,data);
                        recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
                        recyclerView.setAdapter(myPushInvitationAdapter);
                    } else {
                        isEmpty.setVisibility(View.VISIBLE);
                    }

                } else {
                    refreshLayout.setRefreshing(false);
                    Toast.makeText(MyPushInvitation.this, "获取失败", Toast.LENGTH_SHORT).show();
                }
            }
        });


    }

    SwipeMenuCreator swipeMenuCreator = new SwipeMenuCreator() {
        @Override
        public void onCreateMenu(SwipeMenu swipeLeftMenu, SwipeMenu swipeRightMenu, int viewType) {
            int width = getResources().getDimensionPixelSize(R.dimen.dp_70);
            int height = ViewGroup.LayoutParams.MATCH_PARENT;
            SwipeMenuItem deleteItem = new SwipeMenuItem(MyPushInvitation.this)
                                        .setTextColor(Color.BLACK)
                                        .setBackgroundColor(Color.RED)
                                        .setText("删除")
                                        .setWidth(width)
                                        .setHeight(height);
            swipeRightMenu.addMenuItem(deleteItem);
        }
    };
    // 菜单点击监听
    SwipeMenuItemClickListener swipeItemClickListener = new SwipeMenuItemClickListener() {
        @Override
        public void onItemClick(SwipeMenuBridge menuBridge) {
            menuBridge.closeMenu();

            int direction = menuBridge.getDirection();
            int adapterPosition = menuBridge.getAdapterPosition();
            int position = menuBridge.getPosition();
            if(direction == SwipeMenuRecyclerView.RIGHT_DIRECTION){

                Post post = new Post();
                post.setObjectId(data.get(adapterPosition).getObjectId());
                post.delete(new UpdateListener() {
                    @Override
                    public void done(BmobException e) {
                        if(e == null){
                            data.remove(adapterPosition);
                            myPushInvitationAdapter.notifyDataSetChanged();
                        } else {
                            Toast.makeText(MyPushInvitation.this, "删除失败", Toast.LENGTH_SHORT).show();
                        }
                    }
                });


            }
        }
    };

    private void initView() {
        recyclerView = findViewById(R.id.my_push_invitation_recycle_view);
        refreshLayout = findViewById(R.id.my_push_invitation_sf);
        isEmpty = findViewById(R.id.my_push_invitation_is_empty);
        back = findViewById(R.id.my_push_invitation_back);
    }
}
